using System;
using System.Collections.Generic;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using IWS.Lib.SingleSignOnServer;
using System.Web.Security;
using IWS.Lib.SingleSignOnServer.ServiceResponses;


namespace IWS.Lib.SingleSignOnServer.WebServices
{
	/// <summary>
	/// Summary description for RoleService
	/// </summary>
	[WebService(Namespace = "http://www.integratedwebsystems.com/Services/", Description = "This is the role provider service.")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	public class RoleService : System.Web.Services.WebService
	{

		public RoleService()
		{

			//Uncomment the following line if using designed components 
			//InitializeComponent(); 
		}

		protected virtual void LogException(Exception ex)
		{
			//TODO:	override this using a custom logging tool
		}

		private bool ValidateServiceKey(string sServiceKey)
		{
			IWS.Lib.SingleSignOnServer.Service svc = new IWS.Lib.SingleSignOnServer.Service(sServiceKey);
			if (svc != null && svc.ExpirationDate >= DateTime.Now.ToUniversalTime())
			{
				svc.LastActiveDate = DateTime.Now.ToUniversalTime();
				svc.Save();
				return true;
			}

			return false;
		}

		[WebMethod]
		public ServiceResponse AddUsersToRoles(string sServiceKey, string[] usernames, string[] roleNames)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sServiceKey))
				{
					Roles.Provider.AddUsersToRoles(usernames, roleNames);
					resp.Result = ServiceResponseBase.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponseBase.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponseBase.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse GetApplicationName(string sServiceKey)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sServiceKey))
				{
					resp.Data = Roles.Provider.ApplicationName;
					resp.Result = ServiceResponseBase.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponseBase.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponseBase.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}
		[WebMethod]
		public ServiceResponse SetApplicationName(string sServiceKey, string name)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sServiceKey))
				{
					Roles.Provider.ApplicationName = name;
					resp.Result = ServiceResponseBase.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponseBase.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponseBase.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse CreateRole(string sServiceKey, string roleName)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sServiceKey))
				{
					Roles.Provider.CreateRole(roleName);
					resp.Result = ServiceResponseBase.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponseBase.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponseBase.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse DeleteRole(string sServiceKey, string roleName, bool throwOnPopulatedRole)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sServiceKey))
				{
					resp.Data = Roles.Provider.DeleteRole(roleName, throwOnPopulatedRole);
					resp.Result = ServiceResponseBase.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponseBase.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponseBase.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ArrayServiceResponse FindUsersInRole(string sServiceKey, string roleName, string usernameToMatch)
		{
			ArrayServiceResponse resp = new ArrayServiceResponse();
			try
			{
				if (ValidateServiceKey(sServiceKey))
				{
					resp.Data = Roles.Provider.FindUsersInRole(roleName, usernameToMatch);
					resp.Result = ServiceResponseBase.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponseBase.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponseBase.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ArrayServiceResponse GetAllRoles(string sServiceKey)
		{
			ArrayServiceResponse resp = new ArrayServiceResponse();
			try
			{
				if (ValidateServiceKey(sServiceKey))
				{
					resp.Data = Roles.Provider.GetAllRoles();
					resp.Result = ServiceResponseBase.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponseBase.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponseBase.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ArrayServiceResponse GetRolesForUser(string sServiceKey, string username)
		{
			ArrayServiceResponse resp = new ArrayServiceResponse();
			try
			{
				if (ValidateServiceKey(sServiceKey))
				{
					resp.Data = Roles.Provider.GetRolesForUser(username);
					resp.Result = ServiceResponseBase.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponseBase.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponseBase.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ArrayServiceResponse GetUsersInRole(string sServiceKey, string roleName)
		{
			ArrayServiceResponse resp = new ArrayServiceResponse();
			try
			{
				if (ValidateServiceKey(sServiceKey))
				{
					resp.Data = Roles.Provider.GetUsersInRole(roleName);
					resp.Result = ServiceResponseBase.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponseBase.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponseBase.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse IsUserInRole(string sServiceKey, string username, string roleName)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sServiceKey))
				{
					resp.Data = Roles.Provider.IsUserInRole(username, roleName);
					resp.Result = ServiceResponseBase.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponseBase.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponseBase.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse RemoveUsersFromRoles(string sServiceKey, string[] usernames, string[] roleNames)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sServiceKey))
				{
					Roles.Provider.RemoveUsersFromRoles(usernames, roleNames);
					resp.Result = ServiceResponseBase.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponseBase.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponseBase.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}

		[WebMethod]
		public ServiceResponse RoleExists(string sServiceKey, string roleName)
		{
			ServiceResponse resp = new ServiceResponse();
			try
			{
				if (ValidateServiceKey(sServiceKey))
				{
					Roles.Provider.RoleExists(roleName);
					resp.Result = ServiceResponseBase.eResult.Success;
				}
				else
				{
					resp.Result = ServiceResponseBase.eResult.InvalidRegistration;
				}
			}
			catch (Exception ex)
			{
				resp.Message = ex.Message;
				resp.Result = ServiceResponseBase.eResult.Error;

				this.LogException(ex);
			}
			return resp;
		}
	}

}